package com.cvmars.jianyou.module.fragment;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.Consts;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.model.TaskModel;
import com.cvmars.jianyou.module.activity.FindDetailActivity;
import com.cvmars.jianyou.module.activity.TaskDetailActivity;
import com.cvmars.jianyou.module.adapter.FindAdapter;
import com.cvmars.jianyou.module.adapter.HomeAdapter;
import com.cvmars.jianyou.module.adapter.HomeTaskTypeAdapter;
import com.cvmars.jianyou.module.base.BaseFragment;
import com.cvmars.jianyou.module.model.DianZhanModel;
import com.cvmars.jianyou.module.model.EventFindRefresh;
import com.cvmars.jianyou.module.model.FindListModel;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;
import com.cvmars.jianyou.utils.DensityUtils;
import com.cvmars.jianyou.utils.GlideImageLoader;
import com.cvmars.jianyou.utils.ToastUtils;
import com.cvmars.jianyou.widget.ZoomPageTransformer;
import com.youth.banner.Banner;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hehaifeng on 2018/11/26.
 */

public class TaskFragment extends BaseFragment {


    PulltoRefreshRecyclerView listHome;

    FindAdapter homeAdapter;

    boolean isHot;

    List<FindModel> modelList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.frg_home_index, null);

        listHome = view.findViewById(R.id.list_home);

        EventBus.getDefault().register(this);
        homeAdapter = new FindAdapter(getActivity(), R.layout.item_find, modelList);
        Bundle arguments = getArguments();


        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), FindDetailActivity.class);
                intent.putExtra("data", modelList.get(position));
//                getActivity().startActivity(intent);
                
                startActivity(intent);
            }
        });

        listHome.setAdapter(homeAdapter);


        if (arguments != null) {
            View headerView = LayoutInflater.from(getContext()).inflate(R.layout.header_find_ad, null);

//            Banner banner = headerView.findViewById(R.id.viewpager_ad);
//
//            banner.getLayoutParams().height = DensityUtils.getWindowWidth(getActivity()) * 3 / 16;
//            List<String> images = new ArrayList<>();
//            images.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3297378006,4195609120&fm=26&gp=0.jpg");
//            images.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=4273740877,2795510877&fm=26&gp=0.jpg");
//            images.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1373359540,2883307621&fm=26&gp=0.jpg");
//            banner.setImages(images).setImageLoader(new GlideImageLoader()).start();
//
//
//            banner.setPageTransformer(true, new ZoomPageTransformer(0.8f, 1.2f));

//            RecyclerView findAD = headerView.findViewById(R.id.list_find_ad);
//            findAD.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
//
//
//            List<String> data = new ArrayList<>();
//            data.add("1");
//            data.add("1");
//            data.add("1");
//            data.add("1");
//
//            HomeTaskTypeAdapter taskTypeAdapter = new HomeTaskTypeAdapter(R.layout.item_find_ad, data);
//            findAD.setAdapter(taskTypeAdapter);
//            listHome.addHeaderView(headerView);
            getLoadDialogAndShow();
        }

        if (arguments != null) {

            isHot = arguments.getBoolean("isHot", false);
        }

        listHome.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {

                if (isHot) {
                    requestNew();
                } else {

                    request();
                }
            }

            @Override
            public void onRefresh() {

                listHome.mCurPager = 1;
                if (isHot) {
                    requestNew();
                } else {

                    request();
                }

            }
        });


        if (isHot) {
            requestNew();
        } else {

            request();
        }
        listHome.loadMoreEnd();
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventRefresh(EventFindRefresh refresh) {

        if (refresh.isRefresh) {

            listHome.mCurPager = 1;
            request();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventRefresh(DianZhanModel refresh) {

        for (int i = 0; i < modelList.size(); i++) {

            FindModel findModel = modelList.get(i);

            if(findModel.id.equals(refresh.find_id)){

                findModel.is_like = refresh.is_like;
                findModel.comment_total = refresh.comments;
                findModel.like_total = refresh.likes;
            }
        }

        homeAdapter.notifyDataSetChanged();
    }



    private void request() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getFindList(listHome.mCurPager)
                , new SimpleSubscriber<HttpResult<FindListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<FindListModel> o) {

                        getLoadDialogAndDismiss();

                        FindListModel data = o.getData();

                        if (data != null) {

                            List<FindModel> results = data.results;

                            if (results != null) {

                                if (listHome.mCurPager == 1) {
                                    modelList.clear();
                                }
                                modelList.addAll(results);
                                homeAdapter.notifyDataSetChanged();
                                listHome.refreshComplete();
                                listHome.loadMoreComplete();

                            }
                            if (data.next == null) {
                                listHome.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }


    private void requestNew() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getNewFindList(listHome.mCurPager)
                , new SimpleSubscriber<HttpResult<FindListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<FindListModel> o) {

                        getLoadDialogAndDismiss();

                        FindListModel data = o.getData();

                        if (data != null) {

                            List<FindModel> results = data.results;

                            if (results != null) {

                                if (listHome.mCurPager == 1) {
                                    modelList.clear();
                                }
                                modelList.addAll(results);
                                homeAdapter.notifyDataSetChanged();
                                listHome.refreshComplete();
                                listHome.loadMoreComplete();

                            }
                            if (data.next == null) {
                                listHome.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
